<?php

/**
 * Controller_Template_New.
 *
 * @version  $Id: template.php 2 2009-10-02 23:06:43Z perfilev $
 * @package  System
 */

abstract class Controller_Templatenew extends Controller_Template
{
	public $mod;
    public $formIdea;
    public $neadRenew;
	/**
	 * Loads the template View object.
	 *
	 * @return  void
	 */
	public function before()
	{
            $this->template = new View($this->template);
            $this->mod = Model::factory('directory');
            
            $this->template->header = new View('header');
            $this->template->header->active='';
            
            $this->template->footer = new View('footer');
            if(empty($_SESSION)) {
                session_start();
            }
//echo "<pre>";
//unset ($_SESSION['cart']['coupons']);
//print_r($_SESSION);exit;
            
            $this->template->dialog = new View('dialog');
            
            $formIdea = new Form('idea');
            $formIdea->template = 'formSend';
            $formIdea->text('email', '','email(optional)')->rule('email')->rule('maxLength', 32);//->attribute('style', 'width: 300px');
            $formIdea->textarea('idea', '', 'enter sign idea')->rule('required');//->rule('maxLength', 32)->attribute('style', 'width: 300px');

            $formIdeaTempl = new Form('ideaTempl');
            $formIdeaTempl->template = 'formSend';
            $formIdeaTempl->text('email', '','email(optional)')->rule('email')->rule('maxLength', 32);//->attribute('style', 'width: 300px');
            $formIdeaTempl->textarea('idea', '', 'enter sign idea')->rule('required');//->rule('maxLength', 32)->attribute('style', 'width: 300px');
            
            $terms = $this->mod->getItem('212','terms');
            $this->template->dialog->terms = $terms;
            $this->formIdea = $formIdea;
            $this->formNew = $formIdeaTempl;
            $this->template->dialog->formNew = $formIdeaTempl;
            
            $coupons = $this->mod->getItems('coupons');
            $this->template->dialog->codes = $coupons;
            
            $download = $this->mod->getItems('download');
            $downloadAttachments = $this->mod->getAttachmentsByItem('download');
            foreach ($downloadAttachments as $k=>$v) {
                if (isset($v[0])) {
                    $downloadAttachments[$k][0]['path'] = $this->mod->attachmentPath($v[0]);
                }
            }
            $this->template->dialog->download = $download;
            $this->template->dialog->downloadAttachments = $downloadAttachments;
            
            
//            echo '<pre>';
//            print_r($_SESSION);
//            print_r ($downloadAttachments);
            if (isset($_SESSION['cart'])>0) {
                $cart = $_SESSION['cart'];
                $item = array();
                if (isset($cart['catalog'])>0) {
                    foreach ($cart['catalog'] as $key=>$items) {
                        $item[] = $this->mod->getItem($key, 'catalog');
                    }
                }
                if (isset($cart['custom'])>0) {
                    $item[] = array('name'=>'Custom sign', 'price'=>0,'section'=>'1','id'=>'1');
                }
                if (isset($cart['specials'])>0) {
                    foreach ($cart['specials'] as $key=>$items) {
                        $item[] = $this->mod->getItem($key, 'specials');
                    }
                }
                $this->template->dialog->items = $item;
                $this->template->dialog->delivery = array('delivery'=>isset($cart['delivery'])?$cart['delivery']:0, 
                    'deliveryCost'=>(isset($cart['deliveryCost']))?$cart['deliveryCost']:0);
            } else {
                $this->template->dialog->items = array();
                $this->template->dialog->delivery = array('delivery'=>isset($cart['delivery'])?$cart['delivery']:0, 
                    'deliveryCost'=>(isset($cart['deliveryCost']))?$cart['deliveryCost']:0);
            }
            
            $this->addScript('/scripts/jquerytools/jquery.cookie.js');
            $this->addScript('/scripts/jquerytools/jquery.session.js');
            $this->addScript('/scripts/dialog.js');
            $this->addScript('/scripts/jquerytools/jquery.tinycarousel.min.js');
            $this->addScript('/scripts/jquerytools/jquery.cycle.js');
            $this->addScript('/scripts/jquerytools/jquery.mousewheel.js');
            $this->addScript('/scripts/jquerytools/jquery.jscrollpane.min.js');
            $this->addScript('/scripts/main.js');
            
            $this->addStyle('/styles/jquerytools/jquery.jscrollpane.css');
            
            if((Request::getUserAgent('browser') == 'Internet Explorer' and Request::getUserAgent('version') < 8) or
                (Request::getUserAgent('browser') == 'Firefox' and Request::getUserAgent('version') < 4) or
                (Request::getUserAgent('browser') == 'Chrome' and Request::getUserAgent('version') < 6) or
                (Request::getUserAgent('browser') == 'Opera' and Request::getUserAgent('version') < 6)) {
                    $this->neadRenew = true;
            }
	}
    
    public function after() {
        if (isset($_SESSION['cart'])>0) {
            $cart = $_SESSION['cart'];
            $item = array();
            if (isset($cart['catalog'])>0) {
                foreach ($cart['catalog'] as $key=>$items) {
                    $item[] = $this->mod->getItem($key, 'catalog');
                }
            }
            if (isset($cart['custom'])>0) {
                $item[] = array('name'=>'Custom sign', 'price'=>0,'section'=>'1','id'=>'1');
            }
            if (isset($cart['specials'])>0) {
                foreach ($cart['specials'] as $key=>$items) {
                    $item[] = $this->mod->getItem($key, 'specials');
                }
            }
            $this->template->dialog->items = $item;
            $this->template->dialog->delivery = array('delivery'=>isset($cart['delivery'])?$cart['delivery']:0, 
                'deliveryCost'=>(isset($cart['deliveryCost']))?$cart['deliveryCost']:0);
        } else {
            $this->template->dialog->items = array();
            $this->template->dialog->delivery = array('delivery'=>isset($cart['delivery'])?$cart['delivery']:0, 
                'deliveryCost'=>(isset($cart['deliveryCost']))?$cart['deliveryCost']:0);
        }
        parent::after();
    }
}
